﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

using com.vs.General;
using com.vs.General.RF;
using RFSession = com.vs.General.Session;
using com.vs.General.BL;

namespace com.vs.RF.Web
{
    public partial class LoginHandling : System.Web.UI.Page
    {
        string stUser = null;
        string stPassword = null;
        string stCompany = null;
        string stError = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            string stSourcePage = "~/login/Login.aspx";
            string stTargetPage = "~/login/Main.aspx";

            this.parseRequest();
            if(!StringManager.IsEmpty(stError))
                Server.Transfer(stSourcePage + "?ERROR_MESSAGE=" + stError);

            this.procData();
            if (!StringManager.IsEmpty(stError))
                Server.Transfer(stSourcePage + "?ERROR_MESSAGE=" + stError);
            else
                Server.Transfer(stTargetPage);
        }

        protected void parseRequest()
        {
            stUser = Request["tbUser"];
            if (StringManager.IsEmpty(stUser))
            {
                stError = MessageManager.MSG_USR_005;
                return;
            }
            stPassword = Request["tbPassword"];
            stCompany = Request["tbCompany"];
            if (StringManager.IsEmpty(stCompany))
            {
                stError = MessageManager.MSG_USR_006;
                return;
            }
        }

        protected void procData()
        {
            LoginManager mgr = null;
            RFSession sysSess = null;
            RFSession usrSess = null;
            string stConnString = null;

            try
            {
                stConnString = WebConfigurationManager.ConnectionStrings["systemDB"].ConnectionString;
                if (StringManager.IsEmpty(stConnString)){
                    stError = MessageManager.MSG_SES_002;//"错误：初始化Session失败，系统数据库连接未设置";
                    return;
                }

                sysSess = new RFSession();
                sysSess.ConnectionString = stConnString;
                mgr = new LoginManager();
                stError = mgr.Login(sysSess, stUser, stPassword, stCompany);

                if (StringManager.IsErrorMessage(stError))
                    return;
                else
                {
                    stError = null;
                    stConnString = WebConfigurationManager.ConnectionStrings["defaultDB"].ConnectionString;
                    
                    if (StringManager.IsEmpty(stConnString)){
                        stError = MessageManager.MSG_SES_003;//"错误：初始化Session失败，用户数据库连接未设置";
                        return;
                    }

                    usrSess = SessionFactory.CreateSession(stUser, stConnString);
                    Session[RFSupport.SESSION] = usrSess;
                }
            }
            catch (Exception ex)
            {
                stError = ex.Message;
            }
        }
    }
}
